//  -----------------------------------------------------------------------------------------------//
//   This file is part of SvnExternalUpdater                                                               //
//   Copyright (C)2008 Romain Verdier ( romain@codingly.com )                                      //
//                                                                                                 //
//   This program is free software; you can redistribute it and/or modify it under the terms       //
//   of the GNU General Public License as published by the Free Software Foundation; either        //
//   version 2 of the License, or (at your option) any later version. This program is distributed  //
//   in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied        //
//   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  //
//   License for more details.                                                                     //
//                                                                                                 //
//   You should have received a copy of the GNU General Public License along with this program; if //
//   not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. or  //
//   see http://www.fsf.org/licensing/licenses/gpl.html                                            //
//  -----------------------------------------------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Codingly.SvnExternalUpdater.Core.Services;
using SharpSvn;

namespace Codingly.SvnExternalUpdater.Core
{
	public class SvnService : IVcsService
	{
		#region IVcsService Members

		public bool IsValidTarget(string path)
		{
			using (var svnClient = new SvnClient())
			{
				try
				{
					SvnInfoEventArgs infos;
					return svnClient.GetInfo(path, out infos);
				}
				catch (SvnEntryNotFoundException)
				{
					return false;
				}
			}
		}

		public void SetExternalPropertyValue(string path, string value)
		{
			using (var svnClient = new SvnClient())
			{
				svnClient.SetProperty(path, "svn:externals", value);
			}
		}

		public bool IsUpToDate(string path)
		{
			bool result = false;
			using (var svnClient = new SvnClient())
			{
				var args = new SvnStatusArgs {RetrieveRemoteStatus = true};
				Collection<SvnStatusEventArgs> infos;
				if (svnClient.GetStatus(path, args, out infos))
				{
					var statuses = new List<SvnStatusEventArgs>(infos);
					result = statuses.TrueForAll(e => e.RemoteContentStatus == SvnStatus.None && e.RemotePropertyStatus == SvnStatus.None);
				}
			}
			return result;
		}

		public long GetLastChangeRevision(Uri url)
		{
			SvnTarget target = SvnTarget.FromUri(url);
			SvnInfoEventArgs infos;
			using (var svnClient = new SvnClient())
			{
				if (svnClient.GetInfo(target, out infos))
				{
					return infos.LastChangeRevision;
				}
			}
			return -1;
		}

		public LocalFolderInfo GetLocalDirectoryInfo(string path)
		{
			SvnTarget target = SvnTarget.FromString(path);
			SvnInfoEventArgs infos = GetInfo(target);
			LocalFolderInfo result = null;
			if (infos != null)
			{
				string svnExternalsProperty = GetSvnExternalProperty(target);
				result = new LocalFolderInfo(infos.Uri, infos.Revision, infos.RepositoryRoot, svnExternalsProperty);
			}
			return result;
		}

		#endregion

		private static SvnInfoEventArgs GetInfo(SvnTarget target)
		{
			SvnInfoEventArgs infos;
			using (var svnClient = new SvnClient())
			{
				svnClient.GetInfo(target, out infos);
			}
			return infos;
		}

		private static string GetSvnExternalProperty(SvnTarget target)
		{
			string value;
			using (var svnClient = new SvnClient())
			{
				svnClient.TryGetProperty(target, "svn:externals", out value);
			}
			return value;
		}
	}
}